package control;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.talknow.R;
import common.Const;
import common.MyThread;
import common.service.utils.NetworkUtil;

@SuppressLint("HandlerLeak")
public class Screen4Activity extends FragmentActivity implements
		OnClickListener {
	GoogleMap googleMap;
	Location myLocation;
	Location centerLocation;
	Handler _getMyLocationHandler;
	MyThread _myThread;
	Marker marker;
	CameraPosition cameraPosition;
	ProgressBar progressBarLoadAddress;
	double lat = 0;
	double lon = 0;
	Address myaddress = null;
	LocationManager myLocationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_screen_4);
		initWidget();
		getMyLocation();
		initMap();
		
	
		AdView adView = (AdView)this.findViewById(R.id.app_admob);  
		AdRequest request = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).
		        addTestDevice("9234EBAE83E894E6271DEBE7B380782E").build();              
		adView.loadAd(request);
	}
	
	void initMap(){
		final double myLat = 35.746512259918504;
		final double myLon = 139.72412109375;
		cameraPosition = new CameraPosition(
				new LatLng(myLat, myLon), 16, 30, 45);
		googleMap.animateCamera(CameraUpdateFactory
				.newCameraPosition(cameraPosition));

		googleMap.animateCamera(CameraUpdateFactory
				.newCameraPosition(cameraPosition),
				new CancelableCallback() {
					@Override
					public void onFinish() {
						canvasMarker(myLat, myLon);
					}

					@Override
					public void onCancel() {
						canvasMarker(myLat, myLon);
					}
				});
	}

	public void getMyLocation() {

		// custom dialog

		final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_wait);

		_getMyLocationHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				if (googleMap.getMyLocation() != null) {
					dialog.dismiss();
					_myThread.mysuspend();

					myLocation = googleMap.getMyLocation();
					final double myLat = myLocation.getLatitude();
					final double myLon = myLocation.getLongitude();
					cameraPosition = new CameraPosition(
							new LatLng(myLat, myLon), 16, 30, 45);
					googleMap.animateCamera(CameraUpdateFactory
							.newCameraPosition(cameraPosition));

					googleMap.animateCamera(CameraUpdateFactory
							.newCameraPosition(cameraPosition),
							new CancelableCallback() {
								@Override
								public void onFinish() {
									canvasMarker(myLat, myLon);
								}

								@Override
								public void onCancel() {
									canvasMarker(myLat, myLon);
								}
							});

				} else {
					dialog.show();
				}
			}
		};

	}

	/** lon tu man hinh dir truyen sang */

	private void initWidget() {

		((Button) findViewById(R.id.screen_4_button_my_location))
				.setOnClickListener(this);
		((Button) findViewById(R.id.screen_4_button_done))
				.setOnClickListener(this);

		myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		googleMap = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.screen_4_map)).getMap();
		googleMap.setMyLocationEnabled(true);
		googleMap.getUiSettings().setMyLocationButtonEnabled(false);
		googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

		// handel
		googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {

			@Override
			public void onMapLongClick(LatLng arg0) {
				if (NetworkUtil.checkNetWork(getApplicationContext())) {
					canvasMarker(arg0.latitude, arg0.longitude);
				} else {

				}

			}
		});
	}

	public void canvasMarker(double latd, double lond) {
		if (marker != null) {
			googleMap.clear();
			marker.remove();
		}

		lat = latd;
		lon = lond;
		marker = googleMap.addMarker(new MarkerOptions()
				.position(new LatLng(lat, lon))
				.title(lat + "," + lon)
				.icon(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
			
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.screen_4_button_my_location:
			if (NetworkUtil.checkNetWork(getApplicationContext())) {
				if (_myThread != null) {
					_myThread.myresume();
				} else {
					_myThread = new MyThread("", _getMyLocationHandler);
				}
			} 
			break;
		case R.id.screen_4_button_done:
			Intent data = new Intent();
			Bundle bunder = new Bundle();
			bunder.putString(Const.KEY_LAT, String.valueOf(lat));
			bunder.putString(Const.KEY_LON, String.valueOf(lon));
			data.putExtras(bunder);
			setResult(RESULT_OK, data);
			finish();
			break;
		default:
			break;
		}
	}

}
